﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Ams.Extensions;

namespace Ams.Mvc.Bootstrap
{
    public enum Alert
    {
        Success,
        Info,
        Warning,
        Danger
    }

    public static class BootstrapAlertExtensions
    {
        #region Alert
        public static MvcHtmlString Alert(this BootstrapHelper helper, string message)
        {
            return Alert(helper, Bootstrap.Alert.Info, message, null);
        }
        public static MvcHtmlString Alert(this BootstrapHelper helper, Alert alertType, string message)
        {
            return Alert(helper, alertType, message, null);
        }
        public static MvcHtmlString Alert(this BootstrapHelper helper, Alert alertType, string message, object htmlAttributes)
        {
            return Alert(helper, alertType, message, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString Alert(this BootstrapHelper helper, Alert alertType, string message, IDictionary<string, object> htmlAttributes)
        {
            var div = new TagBuilder("div");
            div.InnerHtml = message;

            var attributes = htmlAttributes ?? new Dictionary<string, object>();
            var alertClassName = "alert-" + Enum.GetName(alertType.GetType(), alertType).ToLower();
            if (attributes.ContainsKey("class"))
            {
                var classes = (attributes["class"] ?? string.Empty).ToString().SplitWords().ToList();
                classes.Add("alert");
                classes.Add(alertClassName);
                attributes["class"] = classes.Join(" ");
            }
            else
            {
                attributes.Add("class", "alert " + alertClassName);
            }

            div.MergeAttributes(attributes);

            return div.ToMvcHtmlString();
        } 
        #endregion

        #region AlertSuccess()
        public static MvcHtmlString AlertSuccess(this BootstrapHelper helper, string message)
        {
            return Alert(helper, Bootstrap.Alert.Success, message, null);
        } 
        #endregion

        #region AlertWarning()
        public static MvcHtmlString AlertWarning(this BootstrapHelper helper, string message)
        {
            return Alert(helper, Bootstrap.Alert.Warning, message, null);
        }
        #endregion

        #region AlertDanger()
        public static MvcHtmlString AlertDanger(this BootstrapHelper helper, string message)
        {
            return Alert(helper, Bootstrap.Alert.Danger, message, null);
        }
        #endregion
    }
}
